using System;
using System.Diagnostics;
using MsBuildAxTasks;

namespace MsBuildAxTasks
{
    /// <summary>
    /// Base class for all of the Ax tasks.
    /// </summary>
    public class AxCli
    {
        AxConfigProfile profile;

        private string axversion = "4.0"; // only version 4.0 will be suported in alpha
        private SysLayer _layer;

        #region properties
        public string Logfile { get; set; }
        public string DefaultDir { get; set; }
        public string Layerdevcode { get; set; }
        public string BinPath { get; set; }
        public string Config { get; set; }
        public string Layer
        {
            get { return _layer.ToString(); }
            set { _layer = (SysLayer)Enum.Parse(typeof(SysLayer), value); }
        }        
        #endregion

        #region constructor
        public AxCli() : this("", "")
        {
        }

        public AxCli(string _version) : this(_version, "")
        {
        }

        public AxCli(string _version, string _config)
        {
            axversion = _version;
            Config = _config;
            Init();
        } 
        #endregion

        private void Init()
        {
            profile = new AxConfigProfile(axversion, Config);
            BinPath = GetDefaultCliDir();
        }

        private string GetDefaultCliDir()
        {
            return profile.BinDir;
        }

        private void Open(string _argument)
        {
            string argument = _argument;

            Process proc = new Process();

            // Layer
            if(!string.IsNullOrEmpty(Layer)
               && !string.IsNullOrEmpty(Layerdevcode))
            {
                argument += " -aol=" + Layer;
                argument += string.IsNullOrEmpty(Layerdevcode) ? "" : " -aolcode=" + Layerdevcode;
            }

            // Config
            if(!string.IsNullOrEmpty(Config))
            {
                argument += " -regconfig=" + Config;
            }

            proc.StartInfo.WorkingDirectory = BinPath;
            proc.StartInfo.FileName = "ax32.exe";
            proc.StartInfo.Arguments = argument;
            proc.StartInfo.UseShellExecute = true;
            proc.Start();
            proc.WaitForExit();

            Console.Write("\nAx32 exited with error code\n" + proc.ExitCode);

            proc.Close();           
        }

        public void AutoRun(AutoRunXml _autorun)
        {
            string argument = "";

            // Autorun
            if (_autorun != null)
            {
                _autorun.CreateXml(DefaultDir + @"\autorun.xml");

                argument += " -internal=noModalBoxes -startupcmd=autorun_" + DefaultDir + @"\autorun.xml";
                argument += string.IsNullOrEmpty(DefaultDir) == false ? "," + DefaultDir + @"\" : "";
            }
            Open(argument);
        }

        public void CompileApplication()
        {
            string argument;

            argument = " -startupcmd=compileall_" + DefaultDir + @"\log.xml";

            Open(argument);
        }
    }
}
